package cn.micro.system.manage.service.impl;

import cn.micro.system.manage.common.enums.BusinessTypeEnum;
import cn.micro.system.manage.entity.AssetOdsCustomizedProductsApply;
import cn.micro.system.manage.entity.AssetOdsFileRecordUseRelation;
import cn.micro.system.manage.entity.dto.CustomizedProductsApplySearch;
import cn.micro.system.manage.mapper.AssetOdsCustomizedProductsApplyMapper;
import cn.micro.system.manage.service.AssetOdsCustomizedProductsApplyService;
import cn.micro.system.manage.service.AssetOdsFileRecordUseRelationService;
import cn.micro.system.manage.service.OssService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 定制产品申请表 服务实现类
 * </p>
 *
 * @author wr
 * @since 2024-01-09
 */
@Service
@RequiredArgsConstructor
public class AssetOdsCustomizedProductsApplyServiceImpl extends ServiceImpl<AssetOdsCustomizedProductsApplyMapper, AssetOdsCustomizedProductsApply> implements AssetOdsCustomizedProductsApplyService {

    private final AssetOdsFileRecordUseRelationService useRelationService;

    @Override
    public List<AssetOdsCustomizedProductsApply> listByPage(int page, int size, CustomizedProductsApplySearch search) {
        PageHelper.startPage(page,size);
        return null;
    }

    @Override
    @Transactional
    public boolean apply(AssetOdsCustomizedProductsApply customizedProductsApply) {
        boolean saveResult = this.save(customizedProductsApply);
        if (saveResult && customizedProductsApply.getFileIds() != null){
            List<AssetOdsFileRecordUseRelation> list = new ArrayList<>();
            for (String fileId : customizedProductsApply.getFileIds()) {
                AssetOdsFileRecordUseRelation recordUseRelation = new AssetOdsFileRecordUseRelation();
                recordUseRelation.setFileId(Long.parseLong(fileId));
                recordUseRelation.setUseId(customizedProductsApply.getId());
                recordUseRelation.setBusinessType(BusinessTypeEnum.CUSTOMIZED_PRODUCT_BUSINESS.getValue());
                list.add(recordUseRelation);
            }
            saveResult = useRelationService.saveBatch(list);
        }
        return saveResult;
    }
}
